package com.hxgis.oc.ws;

import com.hxgis.oc.product.base.ProductFactoryManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/webSocket/{sid}")
@Slf4j
@Component
public class WSServer {
    private static WSClientManager wsClientManager;

    private WSClientManager getClientManager(){
        if(wsClientManager == null){
            wsClientManager = (WSClientManager)ProductFactoryManager.getFactory(WSProductFactoryImp.productName);
        }
        return wsClientManager;
    }

    @OnOpen
    public void onOpen(Session session, @PathParam(value = "sid") String sid){
        getClientManager().registerClient(session,sid);
    }

    @OnClose
    public void onClose(Session session, @PathParam(value = "sid") String sid){
        getClientManager().closeClient(session,sid);
    }

    @OnMessage
    public void onMessage(String message) {
        WSClient.getInstance().onMessage(message);
    }

    @OnError
    public void onError(Session session, Throwable throwable){
        log.debug(throwable.getMessage());
    }

}

